package others;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.*;

public class ReadGov {

    public static void main(String[] args) throws IOException {
        try (FileWriter fileWriter = new FileWriter("D:\\data\\govcg\\cgyx3.sql")){
//            fileWriter.write(String.join("\t",
//                    "id",
//                    "site",
//                    "channel",
//                    "title",
//                    "author",
//                    "noticeId",
//                    "noticeType",
//                    "noticeTime",
//                    "channelName",
//                    "siteName",
//                    "openTenderCode",
//                    "url"
//                    ));
//            fileWriter.write("\n");
            for (int i = 1; i <= 33; i++) {
                fileWriter.write(readArticleContent2(i));
                fileWriter.write("\n");
            }
            fileWriter.flush();
        }
    }

    public static String nullToEmpty(String s) {
        return s == null ? "" : s;
    }

    private static String readArticleContent2(int page) {

        StringBuilder sb = new StringBuilder();
        String temp = "insert into cgyx(id, title, budge,btime) values ('#{id}', '#{title}', '#{budge}','#{time}');";
        try (FileReader fis = new FileReader("D:\\data\\govcg\\"+page +".json")
        ){
            HttpClient client = HttpClient.newHttpClient();
            Resps resps = new GsonBuilder()
                    .create()
                    .fromJson(fis, Resps.class);
            List<Row> rows = resps.getData().getRows();
            for (Row row : rows) {
                if ("59".equals(row.getNoticeType()) || "001051".equals(row.getNoticeType())) {
                    // 获取链接地址
                    try {
                        String url = "https://gdgpo.czt.gd.gov.cn/gpcms/rest/web/v2/info/getInfoById?id="+row.getId()+"&_t=1752326895927";
                        HttpRequest request = HttpRequest.newBuilder()
                                .uri(URI.create(url))
                                .GET().build();
                        String body = client.send(request, HttpResponse.BodyHandlers.ofString())
                                .body();
                        Map<String, Object> objectMap = new GsonBuilder()
                                .create().fromJson(body, new TypeToken<Map<String, Object>>() {
                                });
                        Map data = (Map) objectMap.getOrDefault("data", new HashMap<>());
                        Object content = data.get("content");
                        if (content != null) {
                            String string = content.toString();
                            Document document = Jsoup.parse(string);
                            Elements table = document.body().select("table");
                            Elements trs = table.select("tr");
                            if (trs.size() > 1) {
                                for (int i = 1; i < trs.size(); i++) {
                                    String title = trs.get(i).select("td").get(1).text();
                                    String budge = trs.get(i).select("td").get(4).text();
                                    String time = trs.get(i).select("td").get(5).text();
                                    sb.append(temp.replace("#{id}", row.getId())
                                    .replace("#{title}",title.replace("'", "\\'"))
                                            .replace("#{budge}",budge)
                                            .replace("#{time}",time)
                                    );
                            sb.append("\n");
                                }
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }catch (Exception e) {
            System.out.println("Error reading dataframe " + page);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

    private static String readArticleContent(int page) {

        StringBuilder sb = new StringBuilder();
        String temp = "insert into gd_content(id, content) values ('#{id}', '#{content}');";
        try (FileReader fis = new FileReader("D:\\data\\govcg\\"+page +".json")
             ){
            HttpClient client = HttpClient.newHttpClient();
            Resps resps = new GsonBuilder()
                    .create()
                    .fromJson(fis, Resps.class);
            List<Row> rows = resps.getData().getRows();
            for (Row row : rows) {
                if ("59".equals(row.getNoticeType()) || "001051".equals(row.getNoticeType())) {
                    // 获取链接地址
                    try {
                        String url = "https://gdgpo.czt.gd.gov.cn/gpcms/rest/web/v2/info/getInfoById?id="+row.getId()+"&_t=1752326895927";
                        HttpRequest request = HttpRequest.newBuilder()
                                .uri(URI.create(url))
                                .GET().build();
                        String body = client.send(request, HttpResponse.BodyHandlers.ofString())
                                .body();
                        Map<String, Object> objectMap = new GsonBuilder()
                                .create().fromJson(body, new TypeToken<Map<String, Object>>() {
                                });
                        Map data = (Map) objectMap.getOrDefault("data", new HashMap<>());
                        Object content = data.get("content");
                        if (content != null) {
                            sb.append(temp.replace("#{id}", row.getId())
                                    .replace("#{content}",content.toString().replace("'", "\\'")));
                            sb.append("\n");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }catch (Exception e) {
            System.out.println("Error reading dataframe " + page);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }


    private static String readnoticeContent(int page) {
        StringBuilder sb = new StringBuilder();
        String temp = "insert into gd_content(id, content, title, budget, notice_type) values ('#{id}', '#{content}', '#{title}','#{budget}','#{noticeType}');";
        try (FileReader fis = new FileReader("D:\\data\\govcg\\"+page +".json")
        ){
            String ids = "96990f5e-35e8-11f0-8a05-f4b78df327e6\n" +
                    "5d6753cf-3198-11f0-8a05-f4b78df327e6\n" +
                    "70bdffbd-150f-11f0-8a05-f4b78df327e6\n" +
                    "d063ebe5-1395-11f0-8a05-f4b78df327e6\n" +
                    "c5f9df04-f80e-11ef-8a05-f4b78df327e6\n" +
                    "12ff8e65-8d1e-11ef-bf98-f4b78dd1d78b\n" +
                    "fe60b853-7734-11ef-bf98-f4b78dd1d78b\n" +
                    "19145f95-5a1e-11ef-bf98-f4b78dd1d78b\n" +
                    "3b08c079-5940-11ef-bf98-f4b78dd1d78b\n" +
                    "8dbab849-5259-11ef-bf98-f4b78dd1d78b\n" +
                    "4b358ab3-4d90-11ef-bf98-f4b78dd1d78b\n" +
                    "fb98db8d-4982-11ef-bf98-f4b78dd1d78b\n" +
                    "76f84901-32c5-11ef-bf98-f4b78dd1d78b\n" +
                    "66e379fb-1f2c-11ef-bf98-f4b78dd1d78b\n" +
                    "aa4d3574-0dad-11ef-bf98-f4b78dd1d78b\n" +
                    "18f47439-cfd3-11ee-bf98-f4b78dd1d78b\n" +
                    "18bba608-cfd3-11ee-bf98-f4b78dd1d78b\n" +
                    "cd02ce03-bf4f-11ee-bf98-f4b78dd1d78b\n" +
                    "70220f0a-bb5f-11ee-bf98-f4b78dd1d78b\n" +
                    "c4f5deae-bb5b-11ee-bf98-f4b78dd1d78b\n" +
                    "bbea37aa-bb5b-11ee-bf98-f4b78dd1d78b\n";
            HashSet<String> idset = new HashSet<>(Arrays.asList(ids.split("\n")));
            HttpClient client = HttpClient.newHttpClient();
            Resps resps = new GsonBuilder()
                    .create()
                    .fromJson(fis, Resps.class);
            List<Row> rows = resps.getData().getRows();
            for (Row row : rows) {
//                if (!idset.contains(row.getId())) {
//                    continue;
//                }
                if ("95ff31f3-a1af-4bc4-b1a2-54c894476193".equals(row.getChannel())) {
                } else if ("59".equals(row.getNoticeType()) || "001051".equals(row.getNoticeType())) {
                } else {
                    try {
                        String url = "https://gdgpo.czt.gd.gov.cn/gpcms/rest/web/v2/index/selectInfoByOpenTenderCode?" +
                                "channel=" + row.getChannel() +
                                "&site=cd64e06a-21a7-4620-aebc-0576bab7e07a" +
                                "&openTenderCode="+row.getOpenTenderCode()+"&_t=1752327160170";
                        HttpRequest request = HttpRequest.newBuilder()
                                .uri(URI.create(url))
                                .GET()
                                .build();
                        String body = client.send(request, HttpResponse.BodyHandlers.ofString())
                                .body();
                        TenderResp tenderResp = new GsonBuilder()
                                .create()
                                .fromJson(body, TenderResp.class);
                        if (tenderResp != null && tenderResp.getData() != null) {
                            List<TenderResp.Row> rows1 = tenderResp.getData().getRows();
                            if (rows1 != null && !rows1.isEmpty()) {
                                for (TenderResp.Row row1 : rows1) {
                                    sb.append(temp.replace("#{id}", row.getId())
                                            .replace("#{content}",nullToEmpty(row1.getContent()).replace("'", "\\'"))
                                            .replace("#{title}",nullToEmpty(row1.getTitle()).replace("'", "\\'"))
                                            .replace("#{budget}", String.valueOf(row1.getBudget()))
                                            .replace("#{noticeType}", row1.getNoticeType())

                                    );
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("完成了 " + row.getId());
                }
            }
        }catch (Exception e) {
            System.out.println("Error reading dataframe " + page);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

    private static String dataFrame(int page) {
        try (FileReader fis = new FileReader("D:\\data\\govcg\\"+page +".json")){
            Resps resps = new GsonBuilder()
                    .create()
                    .fromJson(fis, Resps.class);
            List<Row> rows = resps.getData().getRows();
            List<String> datas = new ArrayList<>();
            for (Row row : rows) {
                String url = "";
                // 如果channel是95ff31f3-a1af-4bc4-b1a2-54c894476193，则打开articleRedHeadGd
                // 如果noticeType 包括59,001051，则打开articleGd
                // 否则打开noticeGd
                // 内容链接可以搞搞
                if ("95ff31f3-a1af-4bc4-b1a2-54c894476193".equals(row.getChannel())) {
                    url = "https://gdgpo.czt.gd.gov.cn/maincms-web/articleRedHeadGd?id="+row.getId()
                            +"&channelName=" + URLEncoder.encode("项目采购公告", "UTF-8")
                            +"&path=%2FnoticeInformationGd";
                } else if ("59".equals(row.getNoticeType()) || "001051".equals(row.getNoticeType())) {
                    url = "https://gdgpo.czt.gd.gov.cn/maincms-web/articleGd?id="+row.getId()
                            +"&type=article"
                            +"&channelName=" + URLEncoder.encode("项目采购公告", "UTF-8")
                            +"&path=%2FnoticeInformationGd";
                } else {
                    url = "https://gdgpo.czt.gd.gov.cn/maincms-web/noticeGd?id="+row.getId()
                            +"&type=notice"
                            +"&channel=" + row.getChannel()
                            + "&noticeType=" + row.getNoticeType()
                            + "&openTenderCode=" + row.getOpenTenderCode()
                            +"&channelName=" + URLEncoder.encode("项目采购公告", "UTF-8")
                            +"&path=%2FnoticeInformationGd";
                }
                List<String> columns = List.of(
                        nullToEmpty(row.getId()),
                        nullToEmpty(row.getSite()),
                        nullToEmpty(row.getChannel()),
                        nullToEmpty(row.getTitle()),
                        nullToEmpty(row.getAuthor()),
                        nullToEmpty(row.getNoticeId()),
                        nullToEmpty(row.getNoticeType()),
                        nullToEmpty(row.getNoticeTime()),
                        nullToEmpty(row.getChannelName()),
                        nullToEmpty(row.getSiteName()),
                        nullToEmpty(row.getOpenTenderCode()),
                        url
                );
                datas.add(String.join("\t", columns));
            }
            return String.join("\n", datas);
        } catch (Exception e) {
            System.out.println("Error reading dataframe " + page);
            throw new RuntimeException(e);
        }
    }

}
